<template>
  <div class="home-page">
    <van-tabs animated v-model="activeIndex">
      <van-tab
        v-for="channel in channelList"
        :title="channel.name"
        :key="channel.id"
      >
        <article-list :currentId="channel.id"></article-list>
        <!-- 传递当前的channel的id值给list -->
      </van-tab>
    </van-tabs>
    <div class="btn-wrapper">
      <geek-icon name="search"></geek-icon>
      <geek-icon name="channel" @click.native="showCancel=true"></geek-icon>
    </div>
    <article-channel v-model="showCancel" :channellist='channelList' :activeIndex.sync='activeIndex'></article-channel>
  </div>
</template>
<script>
import articleList from './components/articleList.vue'
import { getUserChannels } from '@/api/channel'
import articleChannel from './components/article-channel.vue'

export default {
  name: 'HomePage',
  components: {
    articleList,
    articleChannel
  },
  data () {
    return {
      channelList: [],
      scrollTop: 0,
      showCancel: false,
      activeIndex: 0
    }
  },
  // 创建的时候得到用户数据
  created () {
    this.getChannels()
    this.$plusExtends(() => {
      window.plus.navigator.setStatusBarStyle('dark')
      window.plus.navigator.setStatusBarBackground('#fff')
    })
  },
  // 监听token的变化，重新渲染channels
  watch: {
    '$store.state.user.token': async function () {
      console.log('token变化了')
      const channels = await getUserChannels()
      this.channelList = channels
      this.activeIndex = 0
    }
  },

  methods: {
    // updateChannelIndex (data) {
    //   this.activeIndex = data
    // },
    async getChannels () {
      // const data = await getUserChannels()
      // console.log('我是传递过来的数据data', data)
      // this.channelList = data
      // console.log('channels请求成功', this.channelList)
      this.channelList = await getUserChannels()
    }
  }
}
</script>
<style scoped lang="less">
::v-deep .van-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
  .van-tabs__line {
    background: @geek-color;
    height: 2px;
    width: 32px;
  }
  .van-tab {
    color: #9ea1ae;
  }
  .van-tab--active {
    font-size: 18px;
    color: #333;
  }
  .van-tabs__wrap {
    padding-right: 86px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
  .van-tabs__content {
    flex: 1;
    overflow: hidden;
  }
  .van-tab__pane {
    height: 100%;
  }
}
.home-page {
  .btn-wrapper {
    position: absolute;
    right: 0;
    top: 0;
    width: 86px;
    height: 44px;
    background: #fff;
    display: flex;
    align-items: center;
    .geek-icon {
      flex: 1;
      text-align: center;
      font-size: 18px;
    }
    &::before {
      content: "";
      width: 20px;
      height: 44px;
      position: absolute;
      left: -20px;
      top: 0;
      background: linear-gradient(to right, rgba(255, 255, 255, 0), #fff);
    }
  }
}
</style>
